import axios from 'axios';
import router from '../router';
axios.defaults.timeout = 500000;
axios.defaults.base = process.env.BASE_API ;
axios.defaults.baseURL = process.env.BASE_API+'/manager/1.0/base';
import Vue from 'vue'
import {Message} from '../message/message.js'
import store from '../store'

var num = 0;
var message = {}
//http request 拦截器
axios.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem('token');
    num++;
    if(num == 1){
      // store.commit('changeShow',true)
      message = Message({shows:true})
    }

    config.transformRequest = function (data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    };
    config.headers = {
      "Content-Type":'application/x-www-form-urlencoded; charset=UTF-8'
    };
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);


//http response 拦截器
axios.interceptors.response.use(
  response => {
    num--;
    if(num == 0){
      message.shows = false;
      // store.commit('changeShow',false)
      //message.$el.parentNode.removeChild(message.$el)
    }

    if(response.data.code === 99){
      // console.log(this);
      router.push({
        name: 'login',
        query: {redirect: router.currentRoute.fullPath}
      })
    }
    return response;
  },
  error => {
    num--;
    if(num == 0){
      // console.log(message)
      message.shows = false;
      //message.$el.parentNode.removeChild(message.$el)
    }
    return Promise.reject(error)
  }
)




/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    })
      .then(response => {
        resolve(response.data);

      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url,data = {}){
  return new Promise((resolve,reject) => {
    if(data){
      for(let key in data){
        if(data[key] == undefined) delete data[key]
      }
    }
    // console.log(data,'post')

    axios.post(url,data)
      .then(response => {
        resolve(response.data);

      },err => {
        reject(err);
      })
  })
}

/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function del(url,params={}){
  return new Promise((resolve,reject) => {
    axios.delete(url,{
      params:params
    })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
      .then(response => {
        resolve(response.data);
      },err => {
        reject(err)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
      .then(response => {
        resolve(response.data);
      },err => {
        reject(err)
      })
  })
}
